﻿using oceanho.webapi.factory.httpEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace oceanho.webapi.factory.httpCore.ReqKernal
{
    /// <summary>
    /// 定义一个对ReqCache管理的工厂接口（可以通过实现此接口来改变对请求的ReqCache对象进行管理）
    /// </summary>
    public interface IReqCacheDb
    {
        /// <summary>
        /// 获取ReqCache请求信息缓存数据库中的缓存的ReqCache对象,有缓存返回缓存的ReqCache,否则返回null
        /// </summary>
        /// <param name="request">HttpRequest对象</param>
        ReqCache Get(HttpRequest request);

        /// <summary>
        /// 获取ReqCache请求信息缓存数据库中的缓存的ReqCache对象,有缓存返回缓存的ReqCache,否则new ReqCache返回
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="isCached">对象是否是缓存的（输出参数）</param>
        /// <returns></returns>
        ReqCache Get(string key, out bool isCached);

        /// <summary>
        /// 获取ReqCache请求信息缓存数据库中的缓存的ReqCache对象,有缓存返回缓存的ReqCache,否则返回null
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns></returns>
        ReqCache GetReqCache(string key);

        /// <summary>
        /// 缓存ReqCache到ReqCache请求信息缓存数据库中
        /// </summary>
        /// <param name="request">HttpRequest对象</param>
        /// <param name="key">缓存键</param>
        /// <param name="reqCache">缓存值</param>
        /// <param name="isCached">是否是已经缓存过的值</param>
        /// <param name="isForever">是否永远缓存</param>
        bool Set(HttpRequest request, string key, ReqCache reqCache, bool isCached, bool isForever);

        /// <summary>
        /// 移除ReqCache的IsBadIP标识
        /// </summary>
        /// <param name="reqCache">ReqCache对象</param>
        /// <param name="isForce">是否不检验reqCache.IsBadIP标识进行强制重置IdBadIP</param>
        void RemoveBadIP(ref ReqCache reqCache, bool isForce = false);

        /// <summary>
        /// 将ReqCache的Req_Allow_Clear_IsBadIP_Date添加到最大时间（此ReqCache对象将永远被禁止访问，直到解除为止）
        /// </summary>
        /// <param name="reqCache">ReqCache对象</param>
        void SetBadIPForever(ref ReqCache reqCache);

        /// <summary>
        /// 将ReqCache的IsPassBadIPValid设置为True，标识此ReqCache永远在黑名单IP外
        /// </summary>
        /// <param name="reqCache">ReqCache对象</param>
        void SetIsValidIPForever(ref ReqCache reqCache);

        /// <summary>
        /// 添加/减少请求挂起的次数（addNum大于0，表示添加，否则减少）
        /// </summary>
        /// <param name="reqCache">需要调整的ReqCache对象</param>
        /// <param name="addNum">添加/减少的次数</param>
        void AddReq_Wait_Count(ref ReqCache reqCache, int addNum);

        /// <summary>
        /// 添加/减少单位时间内的请求次数（addNum大于0，表示添加，否则减少）
        /// </summary>
        /// <param name="reqCache">需要调整的ReqCache对象</param>
        /// <param name="addNum">添加/减少的次数</param>
        void AddReq_Seconds_Count(ref ReqCache reqCache, int addNum);

        /// <summary>
        /// 校验IP地址是否不属于黑名单IP
        /// </summary>
        /// <param name="reqCache">需要校验的ReqCache对象</param>
        /// <returns></returns>
        bool IsValidIP(ReqCache reqCache);

        /// <summary>
        /// 清除ReqCacheDb中所有的ReqCache对象
        /// </summary>
        void ClearReqCache();

        /// <summary>
        /// 从ReqCacheDb中删除ReqCache对象
        /// </summary>
        /// <param name="reqKey">ReqCache的键</param>
        bool Remove(string reqKey);
    }
}
